package com.dji.uxsdkdemo;

import static com.dji.uxsdkdemo.SocketClient.getSocketClient;

import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

import dji.common.error.DJIError;
import dji.common.mission.activetrack.ActiveTrackMissionEvent;
import dji.common.realname.AircraftBindingState;
import dji.common.realname.AppActivationState;
import dji.common.useraccount.UserAccountState;
import dji.common.util.CommonCallbacks;
import dji.sdk.base.BaseProduct;
import dji.sdk.flightcontroller.FlightController;
import dji.sdk.mission.activetrack.ActiveTrackMissionOperatorListener;
import dji.sdk.products.Aircraft;
import dji.sdk.realname.AppActivationManager;
import dji.sdk.sdkmanager.DJISDKManager;
import dji.sdk.useraccount.UserAccountManager;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HomeActivity extends DemoBaseActivity implements ActiveTrackMissionOperatorListener, View.OnClickListener {

    private static final String TAG = HomeActivity.class.getName();
    public boolean isLogin=false;
    public boolean isConnect=false;

    private AppActivationManager appActivationManager;
    private AppActivationState.AppActivationStateListener activationStateListener;

    //飞机绑定状态监听器
    private AircraftBindingState.AircraftBindingStateListener bindingStateListener;

    protected Button loginBtn;
    protected Button logout;
    protected Button beginFlyBtn;
    protected Button setting;
    protected TextView bindingStateTV;
    protected TextView appActivationStateTV;

    // 基本地址：服务器ip地址：端口号/Web项目逻辑地址+目标页面（Servlet）的url-pattern
    private static String baseURL = "/analysis/appLogin";
    SocketClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // 当编译和目标版本高于 22 时，在运行时请求
        // 以下权限以确保
        // SDK 正常工作
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.VIBRATE,
                            Manifest.permission.INTERNET, Manifest.permission.ACCESS_WIFI_STATE,
                            Manifest.permission.WAKE_LOCK, Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SYSTEM_ALERT_WINDOW,
                            Manifest.permission.READ_PHONE_STATE,
                    }
                    , 1);
        }


        setContentView(R.layout.activity_home);

        initUI();
        initData();


        try {
            client = SocketClient.getSocketClient(new URI(Utils.ws));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        client.connect();
        onProductConnectionChange();
    }

    protected BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            onProductConnectionChange();
        }
    };

    private void onProductConnectionChange()
    {
        initFlightController();
    }

    private FlightController mFlightController;
    private void initFlightController() {

        BaseProduct product = DemoApplication.getProductInstance();
        if (product != null && product.isConnected()) {
            if (product instanceof Aircraft) {
                mFlightController = ((Aircraft) product).getFlightController();
            }
        }

        if (mFlightController != null) {

            mFlightController.getSerialNumber(new CommonCallbacks.CompletionCallbackWith<String>() {
                @Override
                public void onSuccess(String s) {
                    serial_number = s;
                }

                @Override
                public void onFailure(DJIError djiError) {

                }
            });

        }
    }
    private void initUI(){


        bindingStateTV = (TextView) findViewById(R.id.tv_binding_state_info);
        appActivationStateTV = (TextView) findViewById(R.id.tv_activation_state_info);


        loginBtn = (Button) findViewById(R.id.btn_login);
        loginBtn.setOnClickListener(this);
        logout = (Button) findViewById(R.id.btn_logout);
        logout.setOnClickListener(this);
        setting = (Button) findViewById(R.id.btn_setting);
        setting.setOnClickListener(this);
        beginFlyBtn = (Button) findViewById(R.id.begin_fly);
        beginFlyBtn.setOnClickListener(this);
    }

    String urlBase = "/analysis/appLogin";
    String token = "";

    /**
     * get请求
     */
    public void OkHttpLog(String md5Password,String username) {
        String url =urlBase+"?"+
                "&md5Password=" + md5Password +
                "&username=" + username;
//                "&address=" + mine_04.getText().toString() +
        String token = " ";
        Request.Builder builder = new Request.Builder()
                .url( url )
                .addHeader( "token", token );
        Request build = builder.build();

        OkHttpClient client = new OkHttpClient.Builder().readTimeout( 5000, TimeUnit.SECONDS ).build();
        Call call = client.newCall( build );
        call.enqueue( new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("111111get请求失败======","e.getMessage()"+e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //请求成功返回结果
                //如果希望返回的是字符串
                final String responseData=response.body().string();
                //如果希望返回的是inputStream,有inputStream我们就可以通过IO的方式写文件.
//                InputStream responseStream=response.body().byteStream();
                //注意，此时的线程不是ui线程，
                // 如果此时我们要用返回的数据进行ui更新，操作控件，就要使用相关方法
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 更新UI的操作
                         Log.d("111111get请求成功======",responseData );
                    }
                });
            }
        });
    }


    private String serial_number;
    private String userName;
    private String userPassword;
    private EditText view_url;
    private TextView serial_number_text;
    private EditText userPasswordEdit;
    private View alertDialogView;

    private void setting(View v){
        SharedPreferences sharedPreferences =
                getSharedPreferences("setting", Context.MODE_PRIVATE);

        AlertDialog.Builder loginAlertDialog = new AlertDialog.Builder (HomeActivity.this);
        alertDialogView = getLayoutInflater ().inflate (R.layout.alertdialog_layout, null, false);
        loginAlertDialog.setView (alertDialogView);



        final EditText et = alertDialogView.findViewById (R.id.view_url);
        et.setText(sharedPreferences.getString("url", "请输入直播地址!"));


        serial_number_text = alertDialogView.findViewById (R.id.serial_number);
        serial_number_text.setText(serial_number);
        loginAlertDialog.setPositiveButton ("OK", new DialogInterface.OnClickListener () {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                view_url = alertDialogView.findViewById (R.id.view_url);
                SharedPreferences sharedPreferences = getSharedPreferences("setting", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
                editor.putString("url", view_url.getText ().toString ());
                editor.commit();//提交修改
//                userPasswordEdit = alertDialogView.findViewById (R.id.nian_text);
//                userName = view_url.getText ().toString ();
//                userPassword = userPasswordEdit.getText ().toString ();
            }
        });

        loginAlertDialog.show ();
    }


    private void loginAccount(){

        UserAccountManager.getInstance().logIntoDJIUserAccount(this,
                new CommonCallbacks.CompletionCallbackWith<UserAccountState>() {
                    @Override
                    public void onSuccess(final UserAccountState userAccountState) {
                        Toast.makeText(getApplicationContext(),"登录成功!",Toast.LENGTH_LONG).show();
                        if(userAccountState.name().equals("AUTHORIZED")) {
                            handler.sendMessage(handler.obtainMessage(COMPLETED, "Login"));
                        }
                    }
                    @Override
                    public void onFailure(DJIError error) {
                        Toast.makeText(getApplicationContext(),"登录失败:"
                                + error.getDescription(),Toast.LENGTH_LONG).show();

                    }
                });

    }

    private void logoutAccount(){
        UserAccountManager.getInstance().logoutOfDJIUserAccount(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onResult(DJIError error) {
                if (null == error) {
                    Log.d(TAG,"Logout Success");
                    handler.sendMessage(handler.obtainMessage(COMPLETED, "Logout"));

                } else {
                    Log.d(TAG,"Logout Error:"
                            + error.getDescription());
                }
            }
        });
    }

    private static final int COMPLETED = 0;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == COMPLETED) {
                if(msg.obj.toString().equals("Login")) {
                    appActivationStateTV.setText("已登录"); //UI更改操作
                    isLogin=true;
//                    loginBtn.setText("注销");
                }
                else {
                    appActivationStateTV.setText("未登录");
//                    loginBtn.setText("登录");
                }
            }
        }
    };

    private void initData(){
        setUpListener();

        appActivationManager = DJISDKManager.getInstance().getAppActivationManager();

        if (appActivationManager != null) {
            appActivationManager.addAppActivationStateListener(activationStateListener);
            appActivationManager.addAircraftBindingStateListener(bindingStateListener);
            HomeActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    appActivationStateTV.setText("" + (appActivationManager.getAppActivationState().equals(AppActivationState.ACTIVATED)?"已登录":"未登录"));
                    bindingStateTV.setText( ""+(appActivationManager.getAircraftBindingState().equals(AircraftBindingState.BOUND)?"已连接无人机":"未连接无人机"));
                }
            });
        }
    }


    private void setUpListener() {
        // Example of Listener
        activationStateListener = new AppActivationState.AppActivationStateListener() {
            @Override
            public void onUpdate(final AppActivationState appActivationState) {
                HomeActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        appActivationStateTV.setText(  (appActivationState.equals(AppActivationState.ACTIVATED)?"已登录":"未登录"));
                    }
                });
            }
        };

        bindingStateListener = new AircraftBindingState.AircraftBindingStateListener() {

            @Override
            public void onUpdate(final AircraftBindingState bindingState) {
                HomeActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bindingStateTV.setText(bindingState.equals(AircraftBindingState.BOUND)?"已连接无人机":"未连接无人机");
                    }
                });
            }
        };
    }
    public static void startActivity(Context context, Class activity) {
        Intent intent = new Intent(context, activity);
        context.startActivity(intent);
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_login:{
                loginAccount();
                break;
            }
            case R.id.btn_logout:{
                logoutAccount();
                break;
            }
            case R.id.begin_fly:{
                if(isLogin)
                    startActivity(HomeActivity.this, MainActivity.class);
                else {
                    ToastUtils("请先进行登录!");
                }
                break;
            }
            case R.id.btn_setting:{
                setting(v);
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void onUpdate(ActiveTrackMissionEvent activeTrackMissionEvent) {

    }
    public void  ToastUtils(String string){
        Toast.makeText(getApplicationContext(),string,Toast.LENGTH_LONG).show();
    }

    private void tearDownListener() {
        if (activationStateListener != null) {
            appActivationManager.removeAppActivationStateListener(activationStateListener);
            HomeActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
//                    appActivationStateTV.setText("Unknown");
                }
            });
        }
        if (bindingStateListener !=null) {
            appActivationManager.removeAircraftBindingStateListener(bindingStateListener);
            HomeActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
//                    bindingStateTV.setText("Unknown");
                }
            });
        }
    }

    @Override
    public void onResume() {
        Log.e(TAG, "onResume");
        setUpListener();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        Log.e(TAG, "onDestroy");
//        tearDownListener();
        super.onDestroy();
    }
}
